package com.zllkj.mm.api.network.converter;

import android.util.Log;

import com.google.gson.Gson;
import com.zllkj.mm.api.network.BaseResponse;
import com.zllkj.mm.api.network.exception.ApiErrorCode;
import com.zllkj.mm.api.network.exception.ApiException;
import com.zllkj.mm.model.entity.BaseEntity;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Converter;

public class JsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private final Gson gson;
    private Type type;

    public JsonResponseBodyConverter(Gson gson, Type type) {
        this.gson = gson;
        this.type = type;
    }

    @Override
    public synchronized T convert(ResponseBody value) throws IOException {

        String body = value.string();
        value.close();

        //之前那版处理code带字母的
//        body.split(":\"")[1].split("\"")[0].substring(1);
//        String str1 = body.substring(0, body.indexOf(":\"") + 2);
//        String str2 = body.substring(body.indexOf(":\"") + 4, body.length());
//        body = str1 + str2;

        if (!(type instanceof ParameterizedType)) {
//            value.close();
//            throw new ApiException(ApiErrorCode.ERROR_SERVER_NONE, "app接口错误，没有传入泛型参数", null);
            BaseResponse baseResponse = gson.fromJson(body, BaseResponse.class);
//            if (baseResponse.getCode() != 0) {
//                AppManager.getAppManager().currentActivity().runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        L.showToast(baseResponse.getMessage());
//                    }
//                });
//            }
            return ((T) new Gson().toJson(baseResponse.result));
        }

        Type rawType = ((ParameterizedType) type).getRawType();

        // 正常为Entity
        Type typeArgument = ((ParameterizedType) type).getActualTypeArguments()[0];

        if (rawType == BaseResponse.class) {
            BaseResponse baseResponse = gson.fromJson(body, BaseResponse.class);
            // 如果是基类
            if (typeArgument == BaseEntity.class) {
                value.close();
                BaseEntity baseEntity = new BaseEntity();
                baseEntity.setCode(baseResponse.getCode());
                baseEntity.setMsg(baseResponse.getMessage());
                baseResponse.setResult(baseEntity);
                return (T) baseResponse;
            } else {
//                // 针对data是[]特殊处理//即成功返回对象失败返回数组的特殊处理
//                if (baseResponse.getData() instanceof List && ((List) baseResponse.getData()).size() == 0) {
////                    BaseEntity baseEntity = new BaseEntity();
////                    baseEntity.setCode(baseResponse.getCode());
////                    baseEntity.setMsg(baseResponse.getMessage());
////                    baseResponse.setData(baseEntity);
//                    return (T) baseResponse;
//                } else {
                return gson.fromJson(body, type);
//                }
            }
        } else {
            throw new ApiException(ApiErrorCode.ERROR_SERVER_NONE, "错误的返回数据", null);
        }

    }
}